﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
/// <summary>
/// Summary description for PageBuilder
/// </summary>
public class PagerBuilder
{
    private class PagerLink
    {
        public string Title { get; set; }
        public int PageNo { get; set; }
        public string Class { get; set; }
    }

    private readonly string _urlTemplate;
    private readonly List<PagerLink> _pagerLinks =
            new List<PagerLink>();

    public PagerBuilder(string urlTemplate)
    {
        _urlTemplate = urlTemplate;
    }

    public string PagerClass { get; set; }

    public void AddPage(string title, int pageNo)
    {
        AddPage(title, pageNo, string.Empty);
    }

    public void AddPage(string title, int pageNo,
                        string itemClass)
    {
        var link = new PagerLink
        {
            PageNo = pageNo,
            Title = title,
            Class = itemClass
        };
        _pagerLinks.Add(link);
    }

    public override string ToString()
    {
        var builder = new StringBuilder();

        foreach (var link in _pagerLinks)
        {
            builder.Append("<li>");
            builder.Append("<a");
            if (link.Class != string.Empty)
            {
                builder.AppendFormat(" class=\"{0}\"", link.Class);
            }
            builder.Append(" href=\"");
            builder.Append(_urlTemplate.Replace("$Page$", link.PageNo.ToString()));
            builder.Append("\">");
            builder.Append(link.Title);
            builder.Append("</a>");
            builder.Append("</li>");
        }
        return builder.ToString();
    }
}